package com.fit.pages.degree;

import java.util.List;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.dao.degree.DegreeDAO;
import com.fit.dao.language.LanguageDAO;
import com.fit.entities.Degree;
import com.fit.entities.Language;
import com.fit.pages.Confirmation;
import com.fit.pages.language.AddLanguage;

public class AddDegree {

	@Property
	private List <Degree> degrees;
	
	@Property
	private Degree degree;
	
	@Persist
	@Property
	private String msg;
	
	@Inject
	private DegreeDAO degreeDAO;
	
	@InjectPage
	private Confirmation confirmation;
	
	@InjectPage
	private AddDegree addDegree;
	
	void onActivate(){
		degrees = degreeDAO.findAll();
	}
	
	
	@CommitAfter
	Object onSuccess() {
		confirmation.setError("");
		confirmation.setConfirmation("");
		Degree temp = degreeDAO.findByID(degree.getId());
		
		if (temp == null) {
			if (degreeDAO.save(degree)) {
				//confirmation.setConfirmation(u"Language successfully inserted!");
				msg="Degree successfully inserted!";
				return addDegree;
			}
			confirmation.setConfirmation("Language is null!");
			return confirmation;
		}

		confirmation.setError("Language with " + degree.getId()
				+ " ID already exists!");

		return confirmation;
	}
	
	@CommitAfter
	@OnEvent(component = "deleteLink")
	Object onDeleteLink(Long id) {
		try {
			degreeDAO.deleteByID(id);
			msg="Degree successfully deleted!";
			return null;
		} catch (Exception e) {
			confirmation.setConfirmation("Greska prilikom brisanja zapisa korisnika! ");			
			return confirmation;
		}
	}
	
}
